package com.mz.mattress.admin.api.admin;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.mz.mattress.admin.api.req.*;
import com.mz.mattress.admin.common.Holder;
import com.mz.mattress.admin.service.MerchantService;
import com.mz.mattress.api.common.Constants;
import com.mz.mattress.api.common.Result;
import com.mz.mattress.api.entity.Merchant;
import com.mz.mattress.api.entity.MerchantCheckRecord;
import com.mz.mattress.api.jwt.AdminJwt;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Slf4j
@RestController
@RequestMapping(Constants.ADMIN_PATH + "merchant")
public class MerchantController {

    @Autowired
    private MerchantService merchantService;

    @GetMapping("/listPage")
    public Result<IPage<Merchant>> listPage(MerchantQueryReq req) {
        return merchantService.listPage(req);
    }

    @GetMapping("/exportList")
    public void exportList(HttpServletResponse response, MerchantQueryReq req) {
        merchantService.exportList(response, req);
    }

    @GetMapping(value = "/{id}")
    public Result<Merchant> detail(@PathVariable Integer id) {
        return merchantService.getById(id);
    }

    @PostMapping("/check")
    public Result check(@RequestBody MerchantCheckReq req) {
        AdminJwt userJwt = Holder.ADMIN_JWT_THREAD_LOCAL.get();
        return merchantService.check(req, userJwt.getUserId(), userJwt.getUsername());
    }

    @GetMapping(value = "/records/{id}")
    public Result<List<MerchantCheckRecord>> records(@PathVariable Integer id) {
        return merchantService.records(id);
    }

    @PutMapping
    public Result update(@RequestBody MerchantApplyReq req) {
        return merchantService.updateById(req);
    }

    @DeleteMapping
    public Result delete(@RequestParam Integer id) {
        return merchantService.deleteById(id);
    }

    @PostMapping("/bindInvestor")
    public Result bindInvestor(@RequestBody MerchantInvestorBindReq req) {
        return merchantService.bindInvestor(req);
    }

    @PostMapping("/updateCard")
    public Result updateCard(@RequestBody Merchant merchant) {
        return merchantService.updateCard(merchant.getId(), merchant.getCard());
    }

    @PostMapping("/bindSaleByMobile")
    public Result bindSaleByMobile(@RequestBody MerchantSaleBindReq req) {
        return merchantService.bindSaleByMobile(req.getId(), req.getSaleMobile());
    }

    @PostMapping("/downUpMerchant")
    public Result downUpMerchant(@RequestBody MerchantDownUpReq req) {
        AdminJwt userJwt = Holder.ADMIN_JWT_THREAD_LOCAL.get();
        return merchantService.downUpMerchant(req, userJwt.getUserId(), userJwt.getUsername());
    }

}
